package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.model.Card;
import com.qf.model.Massage;
import com.qf.model.User;
import com.qf.service.OpenService;
import com.qf.service.UserService;
import com.qf.service.impl.OpenServiceImpl;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/OpenServlet")
public class  OpenServlet extends BascServlet {
    OpenService openService = new OpenServiceImpl();
    UserService userService = new UserServiceImpl();


    public void openAccount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        获取请求参数

        Integer uid = Integer.parseInt(request.getParameter("u_id"));
        //根据uid查找到当前登录对象的所有信息
        User user = userService.findByUid(uid);
        Massage<User> m = null;
        if (user != null) {
            //成功
            m = new Massage<>(1, "成功", user);

        } else {

            m = new Massage<>(-1, "未找到");
        }
        String s = JSON.toJSONString(m);
        response.getWriter().write(s);

    }

    public void appOpenCard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer u_id = Integer.parseInt(request.getParameter("u_id"));
        String c_account = request.getParameter("c_account");
        String c_phone = request.getParameter("c_phone");
        String pwd1 = request.getParameter("c_pwd");
        Massage<Card> m = null;
        String accunt = "";
        if (c_account.equals("-1")) {
            m = new Massage<>(-1, "未选择开户行");
        } else {
            if (c_account.equals("0")) {
                accunt = "中国银行";

            } else if (c_account.equals("1")) {
                accunt = "中国邮政";
            } else if (c_account.equals("2")) {
                accunt = "中国农业银行";
            }
            Card card = new Card();
            card.setU_id(u_id);
            card.setC_account(accunt);
            card.setC_phone(c_phone);
            card.setC_pwd(pwd1);
            int i = openService.addOpenCard(card);
            if (i > 0) {
                //成功
                m = new Massage<>(1, "开卡成功，审核时间为两个工作日以内，请您耐心等待");

            } else {

                m = new Massage<>(-1, "开卡失败");
            }


        }
        String s = JSON.toJSONString(m);
        response.getWriter().write(s);

    }
}

